package com.employ.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.employ.admin.common.Result;
import com.employ.admin.entity.Menu;
import com.employ.admin.entity.MenuDo;
import com.employ.admin.entity.MenuVo;
import com.employ.admin.entity.User;
import com.employ.admin.mapper.UserMapper;
import com.employ.admin.service.MenuDoService;
import com.employ.admin.service.MenuService;
import com.employ.admin.service.UserService;
import com.employ.admin.utils.Base64;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserMapper userMapper;

    @Resource
    UserService userService;
    @Resource
    MenuService menuService;

    @Resource
    MenuDoService menuDoService;

    @PostMapping
    public Result<?> save(@RequestBody User user) {
        if (user.getPassword() == null) {//默认密码
            user.setPassword("123468");
        }
        userService.insert(user);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "") String searchStr) {
        /**
         * searchStr 空字符串的处理 导入三方库 hutool
         */
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (StringUtils.isNotBlank(searchStr)) {//searchStr不为空，再设置为模糊查询
            wrapper.like(User::getNickname, searchStr);
        }
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        //拿到数据后渲染vue页面，传给tableData
        return Result.success(userPage);
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {

        userMapper.deleteById(id);
        return Result.success();
    }


    @GetMapping("/sys/userInfo")
    public Result<?> getUserInfo(@RequestBody User user) {

        User us = userMapper.findUserByName(user.getUsername());
        return Result.success(userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername())));
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {

        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res != null) {
            return Result.error("-1", "用户名重复");
        }
        Base64 b6 = new Base64();

        if (user.getPassword() == null) {//默认密码
            user.setPassword(b6.getBase64("123468"));
        }

        userService.insert(user);
        return Result.success();
    }

    @GetMapping("/sys/menu/nav")
    @ResponseBody
    public Result<List<Menu>> getSysMenuNavs() {

        List<Menu> menuDos = (List)menuService.listSysMenuTree();
        System.out.println("---menus:" + menuDos);
        return Result.success(menuDos);
    }

    @GetMapping("/sys/menuDo/list")
    @ResponseBody
    public Result<List<MenuDo>> getSysMenus() {
        List<MenuDo> menuDos = (List<MenuDo>) menuDoService.listMenuTree();
        System.out.println("---menuDos:" + menuDos);
        return Result.success(menuDos);
    }
}
